// pages/my/share-info/share-info.js
const app = getApp();
Page({
  data: {
    isShow: true,
    money: '***',
    records: [],
    applyList: [] // 提现记录
  },
  apply() {
    // 提现
    const that = this;
    if (this.data.money == 0) {
      wx.showToast({
        title: '余额为0，无法提现',
        icon: 'none'
      });
      return;
    }
    wx.showModal({
      title: '申请提现',
      content: '提现需审核，审核完毕后将自动发放至微信钱包',
      confirmText: '确认提现',
      complete: (res) => {
        if (res.cancel) return;

        app.request('/api/user/applyMoney', {}, 'POST').then(res => {
          if (res.data.code === 1) {
            wx.showToast({
              title: '提现成功，等待审核',
              icon: 'none'
            });
            that.loadData();
          } else {
            wx.showToast({
              title: '提现失败',
              icon: 'none'
            });
          }
        }).catch(err => {
          console.error(err);
        });
      }
    })
  },
  ice() {
    const that = this;
    wx.setStorage({
      key: 'showMoney',
      data: !this.data.isShow,
      success() {
        that.setData({
          isShow: !that.data.isShow
        });
      }
    });
  },
  onLoad() {
    const that = this;
    that.loadData();
    wx.getStorage({
      key: 'showMoney',
      success (res) {
        console.log('showMoney', res)
        that.setData({
          isShow: res.data
        });
      }
    });
  },
  loadData() {
    const that = this;
    app.request('/api/user/money').then(res => {
      let newArr = [];
      for (let i = 0;i < res.data.data.list.length;i++) {
        if (res.data.data.list[i].memo === '管理员变更金额') {
          continue;
        } else {
          newArr.push(res.data.data.list[i]);
        }
      }
      for (let i = 0;i < newArr.length;i++) {
        let mobile = newArr[i].map_mobile;
        newArr[i].format_mobile = mobile.substring(0, 3) + '****' + mobile.substring(7, 11);
      }
      that.setData({
        money: res.data.data.money,
        records: newArr
      });
      console.log('list', newArr)
    }).catch(err => {
      console.error(err);
    });

    app.request('/api/user/applyList').then(res => {
      console.log('list', res.data.data)
      that.setData({
        applyList: res.data.data
      })
    }).catch(err => {
      console.error(err);
    });
  },
  gorulesdesc() {
    wx.navigateTo({
      url: './rules-desc/rules-desc',
    })
  },
  onShareAppMessage() {
    return {
      imageUrl: 'https://6578-exam-4gpi8ydje868ff67-1327884539.tcb.qcloud.la/img/share.png',
      path: 'pages/index/index?invite_user_id=' + app.globalData.userinfo.id,
      title: '上海建筑岗位刷题'
    }
  },
})